package krillr.Util;
import robocode.*;
import robocode.util.Utils;
import java.util.*;
import krillr.Predictors.*;
import java.awt.Color;
import java.awt.geom.*;

/**
 * UnitTracker - AdvancedRobot tracking class for Robocode
 * 
 * @author Aaron M. Krill
 * @version 0.1
 */
public class UnitTracker extends Tracker {    
    public UnitTracker() {
        return;
    }
    
    /**
     * Constructs a point at the specified x and y coordinates.
     * 
     * @param e - the ScannedRobotEvent to us by the Battlefield tracker or the bot
     * @param b - the parent robot
     */
    public UnitTracker(ScannedRobotEvent e, KrillBot b) {
        bot = b;
        fieldRectangle = new Rectangle2d(0, 0, b.getBattleFieldWidth(), b.getBattleFieldHeight());
        name = e.getName();
        setVector(new Vector2d(e.getHeadingRadians(), e.getVelocity()));
        setEnergy(energy = oldEnergy);
        setLocation(new Point2d(bot.getX() + e.getDistance() * Math.sin(e.getBearingRadians()), bot.getY() + e.getDistance() * Math.cos(e.getBearingRadians())));
        Predictor p = new PatternMatcherA(bot, this);
        p.update();
        predictors.add(p);
    }
        
    public void run() {
        if(hasFired()) {
            oldEnergy = energy;
            double be = bulletEnergy();
            Point2d bulletLocation = lastKnownLocation();
            double enemyHeading = heading();
            double bulletHeading = KrillUtil.getBearing(bulletLocation, new Point2d(bot.getX(), bot.getY())) + enemyHeading;
            BulletTracker bullet = new BulletTracker(bulletLocation, bulletHeading, be, bot.getTime());
            bullets.add(bullet);
        }
        
        checkBullets();
    }
    
    public void update(ScannedRobotEvent e) {
        setVector(new Vector2d(e.getHeadingRadians(), e.getVelocity()));
        setEnergy(e.getEnergy());
        setLocation(new Point2d(bot.getX() + e.getDistance() * Math.sin(e.getBearingRadians()), bot.getY() + e.getDistance() * Math.cos(e.getBearingRadians())));
        Predictor p = (Predictor)predictors.get(0);
        p.update();
        predictors.set(0,p);
    }
    
    public Point2d myLocation() { return new Point2d(bot.getX(), bot.getY()); }
    public double myHeading() { return bot.getHeadingRadians(); }
}